/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import dal.CauHoiDAO;
import dto.CauHoiDTO;

/**
 *
 * @author Stanley
 */
public class ModuleImportExportQuestionController {

    private CauHoiDAO dao = null;

    public ModuleImportExportQuestionController() {
        this.dao = new CauHoiDAO();
    }

    public boolean exportQuestion(String path) {
        try {
            ArrayList dsCauhoi = dao.getAllQuesttion();
            OutputStreamWriter buff = new OutputStreamWriter(new FileOutputStream(path), "UTF-8");
            buff.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<Danhsach>\r\n");

            for (int i = 0; i < dsCauhoi.size(); i++) {
                CauHoiDTO info = (CauHoiDTO) dsCauhoi.get(i);

                // write to xml
                buff.write("<Cauhoi>\r\n");
                buff.write("\t<Noidung>");
                buff.write(info.getNoidung());
                buff.write("</Noidung>\r\n");
                buff.write("\t<Luachon>");
                buff.write(info.getLuachon1());
                buff.write("</Luachon>\r\n");
                buff.write("\t<Luachon>");
                buff.write(info.getLuachon2());
                buff.write("</Luachon>\r\n");
                buff.write("\t<Luachon>");
                buff.write(info.getLuachon3());
                buff.write("</Luachon>\r\n");
                buff.write("\t<Luachon>");
                buff.write(info.getLuachon4());
                buff.write("</Luachon>\r\n");
                buff.write("\t<Dapan>");
                buff.write(Integer.toString(info.getDapan()));
                buff.write("</Dapan>\r\n");
                buff.write("\t<Dokho>");
                buff.write(Integer.toString(info.getDokho()));
                buff.write("</Dokho>\r\n");
                buff.write("</Cauhoi>\r\n");
            }
            buff.write("</Danhsach>\r\n");
            buff.close();
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}
